<?php
include_once('db.php');

function getAllDepartment(){
	$sql = "select * from _department";

	$result = queryStr($sql);

	if($result['num_rows'] != 0){
		return $result['rows'];
	}

	return '';
}

//通过学院id获取所有该学院的专业
function getAllMajorByDepId($majorId){
	//强制类型转换
	$majorId = (int)$majorId;

	$sql = "select * from `_major` where `department_id`=$majorId";
	
	$result = queryStr($sql);

	if($result['num_rows'] != 0){
		return $result['rows'];
	}

	return '';
}

function getMajorNameByStudentId($id){
	$sql = "SELECT major_id FROM _student WHERE id like '$id'";

	$result = queryStr($sql);

	if($result['num_rows'] != 0){
		return $result['rows'][0]['major_id'];
	}

	return '';
}




//=============================dao====================================

$act = @$_REQUEST['act'];

switch($act){
	case 'ajax':
		ajax();
		break;
	case 'major':
		major();
		break;
}


//用于实现学院-专业二级联动
function ajax(){
	$department_name = $_POST['department_name'];
	$department_val = $_POST['department_val'];
	
	if($department_name != '' && $department_val != ''){
		//返回格式{'num':1, 'rows':[{'major_id':1, 'major_name':'hardware'}, {'major_id':2, 'major_name':'software'}]}
		$json = json_encode(getAllMajorByDepId($department_val));

		echo $json;
	}
}



function major(){
	$id = $_POST['id'];

	$major_id = getMajorNameByStudentId($id);

	echo $major_id;
}



/*      测试代码         */
function test(){
	$str = getAllMajorByDepId(1);
	
	$json = json_encode($str);
	
	echo $json;
}

//test();
/*      测试代码         */
?>